import java.awt.Rectangle;
import java.util.Random;

import ij.IJ;
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.plugin.PlugIn;
import ij.plugin.filter.PlugInFilter;
import ij.process.Blitter;
import ij.process.ImageProcessor;
import ij.process.ShortProcessor;

/**
 * 
 */

/**
 * @author Francesco
 *
 */
public class PoincareArc_ implements PlugInFilter {
	private int imgCols,imgRows; 
	
	private Random generator=new Random();
	
	
	/* (non-Javadoc)
	 * @see ij.plugin.filter.PlugInFilter#run(ij.process.ImageProcessor)
	 */
	public void run(ImageProcessor ip) {
		 //get width, height and the region of interest
			
			int w = ip.getWidth();     
			int h = ip.getHeight();   
			this.imgCols=w;
			this.imgRows=h;
			
			double u1=0.5; 
			double u2=0.5; 
			
			double v1=0.5; 
			double v2=-0.5;
			
			double usquare=u1*u1+u2*u2; 
			double vsquare=v1*v1+v2*v2;
			
			double denom=u1*v2-u2*v1;
			double numX=u2*vsquare-v2*usquare+u2-v2; 
			double numY=v1*usquare-u1*vsquare+v1-u1;
			
			double ax=numX/denom; 
			double by=numY/denom;
			
			double centerX=-ax/2; 
			double centerY=-by/2;
			
			double sX=centerX-u1; 
			double sY=centerY-u2; 
			
			System.out.println(centerX+" "+centerY);
			
		
			double radius=Math.sqrt(sX*sX+sY*sY); 
			
			System.out.println("Rayon :"+radius);
			
			double denomU=(u1-centerX)*(u1-centerX)+(u2-centerY)*(u2-centerY);
			double denomV=(v1-centerX)*(v1-centerX)+(v2-centerY)*(v2-centerY);
			
			System.out.println(denomU+" "+denomV); 
			// TODO : calculer correctement les angles
			//double theta1=Math.acos((centerX-u1)/Math.sqrt(denomU)); 
			// TODO faux !
			//double theta2=-Math.acos((v1-centerX)/denomV);
			double theta1=0;
			double theta2=2*Math.PI; 
			double pas=(theta2-theta1)/2000; 
			System.out.println(theta1+" "+theta2); 
			
			
			System.out.println(w*h+" "+w+" "+h); 

			// create a new image with the same size and copy the pixels of the original image
			ImagePlus inverted = NewImage.createRGBImage ("Image", w, h, 1, NewImage.FILL_BLACK);
		
			ImageProcessor inv_ip = inverted.getProcessor();
			inv_ip.copyBits(ip,0,0,Blitter.COPY);
			int[] pixels=new int[this.imgCols*this.imgRows];
			for(int i=0;i<w;i++)
				for(int j=0;j<h;j++){
					double ni=2.0*i/w-1;
					double nj=2.0*j/h-1;
				
				if(ni*ni+nj*nj<1)
							pixels[i*w+j]=200*256+12+65536*255;
				}
			double angle=theta1; 
			for(int i=0;i<2000;angle+=pas,i++){
				double xp=centerX+radius*Math.cos(angle); 
				double yp=centerY+radius*Math.sin(angle); 
				System.out.println(xp+" "+yp);
				double newX=(xp+1)*imgRows/2; 
				double newY=(yp+1)*imgCols/2; 
				int InewX=(int)newX; 
				int InewY=(int)newY;
				System.out.println(InewX+" "+InewY+" "+angle); 
				if(InewX*imgCols+InewY<w*h){
					pixels[(InewX*imgCols+InewY)]=255;
					
				}
			}
			
			/*for(int i=0;i<w;i++)
				for(int j=0;j<h;j++)
					pixels[i*imgCols+j]=255;*/
			
			inv_ip.setPixels(pixels); 
			 inverted.show();
			 inverted.updateAndDraw();
	}
			
			
			 

	/* (non-Javadoc)
	 * @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
	 */
	
	public int setup(String arg, ImagePlus imp) {
		if (arg.equals("about"))
			{showAbout(); return DONE;}
	            return DOES_RGB+NO_CHANGES;

	}
	
	void showAbout() {
		IJ.showMessage("Poincare's Arc",
			"Tracing a Poincare's arc between two points"
		); 

	}
}